﻿<cfsilent>
	<cfscript>
		
		mathAdvice = getProperty("serviceFactory").getBean("senateMathAdvice");
		
		termId = event.getArg("TID");
	
		taskId = event.getArg("TSK");
		taskType = event.getArg("TT");
		
		/* 可用排课时间单元 */
		timeTable = event.getArg("timeTable");
		
		/*
			课表时间数组 [weekday][lecture]
			
			1  - 可排课
			-1 - 当前教学任务时间占用
			-2 - 当前教学任务延迟占用
			-3 - 班级其他课程
			-4 - 班级其他课程延迟
			-5 - 按人排课 学生上课冲突
			-6 - 教师课表
			-7 - 教师课表延迟占用
		*/


		/* 载入排课任务 和 教学班设置 */
		sql = "SELECT  a.period_done,
						   a.period_need 
					FROM t_task_arrange a 
					WHERE a.task_id = :taskId ";
					
		queryObj = new Query(datasource = application.dnsSlave);
		queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
		rs_taskArrange= queryObj.execute(sql = sql).getResult();
		
		switch(taskType){
					case 'C':
						sql = "SELECT
								b.institute_id,
								a.task_period,
								case when a.task_time is null then '-' else a.task_time end task_time, 
								case when a.task_place is null then '-' else a.task_place end task_place,
								b.course_name,
								c.tch_name,
								a.task_week,
								a.tch_id,
								a.task_mode,
								b.cid,
								a.cidx,
								d.institute_name,
								b.period,
								b.period_theory,
								b.course_credit,
								a.task_desc,
								a.cr_type,
								e.campus_name,
								a.task_limite,
								a.task_chooesd,
								a.campus_id,
								f.cr_type_name
							FROM
								  t_common_task a 
								  INNER JOIN t_course b ON b.cid = a.cid 
								  INNER JOIN t_teacher c ON c.tch_id = a.tch_id
								  INNER JOIN t_institute d ON d.institute_id = b.institute_id 
								  INNER JOIN t_campus e ON e.campus_id = a.campus_id 
								  INNER JOIN t_classroom_type f ON f.cr_type = a.cr_type 
							WHERE
								 a.tsk_id = :taskId ";
						break;
					case 'P':
						sql = "SELECT
								b.institute_id,
								a.task_period,
								case when a.task_time is null then '-' else a.task_time end task_time, 
								case when a.task_place is null then '-' else a.task_place end task_place,
								b.course_name,
								c.tch_name,
								a.task_week,
								a.tch_id,
								a.task_mode,
								b.cid,
								a.cidx,
								d.institute_name,
								b.period,
								b.period_theory,
								b.course_credit,
								a.task_desc,
								a.cr_type,
								e.campus_name,
								a.task_limite,
								a.task_chooesd,
								a.task_classes,
								a.campus_id,
								f.cr_type_name
							FROM
								  t_task a 
								  INNER JOIN t_course b ON b.cid = a.cid 
								  INNER JOIN t_teacher c ON c.tch_id = a.tch_id
								  INNER JOIN t_institute d ON d.institute_id = b.institute_id 
								  INNER JOIN t_campus e ON e.campus_id = a.campus_id
								  INNER JOIN t_classroom_type f ON f.cr_type = a.cr_type  
							WHERE
								 a.tsk_id = :taskId ";
						break;
					case 'R':
						sql = "SELECT
								b.institute_id,
								a.task_period,
								case when a.task_time is null then '-' else a.task_time end task_time, 
								case when a.task_place is null then '-' else a.task_place end task_place,
								b.course_name,
								c.tch_name,
								a.task_week,
								a.tch_id,
								a.task_mode,
								b.cid,
								a.cidx,
								d.institute_name,
								b.period,
								b.period_theory,
								b.course_credit,
								a.task_desc,
								a.cr_type,
								e.campus_name,
								a.task_limite,
								a.task_chooesd,
								f.cr_type_name,
								a.campus_id
							FROM
								  t_reopen_task a 
								  INNER JOIN t_course b ON b.cid = a.cid 
								  INNER JOIN t_teacher c ON c.tch_id = a.tch_id
								  INNER JOIN t_institute d ON d.institute_id = b.institute_id 
								  INNER JOIN t_campus e ON e.campus_id = a.campus_id
								  INNER JOIN t_classroom_type f ON f.cr_type = a.cr_type  
							WHERE
								 a.tsk_id = :taskId ";
						break;
					case 'T':
						sql = "SELECT
								b.institute_id,
								a.task_period,
								case when a.task_time is null then '-' else a.task_time end task_time, 
								case when a.task_place is null then '-' else a.task_place end task_place,
								b.course_name,
								c.tch_name,
								a.task_week,
								a.tch_id,
								a.task_mode,
								b.cid,
								a.cidx,
								d.institute_name,
								b.period,
								b.period_theory,
								b.course_credit,
								a.task_desc,
								a.cr_type,
								e.campus_name,
								a.task_limite,
								a.task_chooesd,
								f.cr_type_name,
								a.campus_id
							FROM
								  t_sport_task a 
								  INNER JOIN t_course b ON b.cid = a.cid 
								  INNER JOIN t_teacher c ON c.tch_id = a.tch_id
								  INNER JOIN t_institute d ON d.institute_id = b.institute_id 
								  INNER JOIN t_campus e ON e.campus_id = a.campus_id
								  INNER JOIN t_classroom_type f ON f.cr_type = a.cr_type  
							WHERE
								 a.tsk_id = :taskId ";
						break;
				}
				
				queryObj = new Query(datasource = application.dnsSlave);
				queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
				rs_task = queryObj.execute(sql = sql).getResult();
		
		/* 校区列表 */
		sql = "SELECT 
					a.campus_id, a.campus_name
					FROM t_campus a  
					ORDER BY a.campus_id";
					
		queryObj = new Query(datasource = application.dnsSlave);
		rs_campus = queryObj.execute(sql = sql).getResult();	
		
		/* 通用教室类别 */
		sql = "SELECT 
				a.cr_type, a.cr_type_name 
				FROM t_classroom_type a 
				ORDER BY a.cr_type";
				
		queryObj = new Query(datasource = application.dnsSlave);
		rs_clsroomType = queryObj.execute(sql = sql).getResult();	
		
		/* 专用教室类别 */
		sql = "SELECT 
			  		a.cr_type, a.cr_type_name 
				FROM t_classroom_type a  
				WHERE 
					a.cr_type LIKE 'Z__' 
					AND 
					a.spec_deps LIKE :condition 
				ORDER BY a.cr_type";
				
		queryObj = new Query(datasource = application.dnsSlave);
		queryObj.addParam(name ="condition", value='%' & rs_task.institute_id & '%' , cfsqltype="cf_sql_varchar");
		rs_clsroomTypeSpecial = queryObj.execute(sql = sql).getResult();
		
		timeTableSet = structNew();
		
		for( lec=1; lec lte 6; lec++ ){
			
			allowCells = mathAdvice.convertLectureIdxToCellLength( lec );
			needCells = listToArray( rs_task.task_period, "+" );
			
		
			
			usableCells = [];
			
			for ( cell in needCells ) {
				if ( arrayFind( allowCells, cell ) and not arrayFind( usableCells, cell ) ) {
					arrayAppend( usableCells, cell );
				}
			}
			
			for( wkday=1; wkday lte 7; wkday++ ){
				lectureKey = "CELL" & wkday & lec;
				structInsert( timeTableSet, lectureKey, usableCells );
			}
		}
		
		
		/* -------------------------------------- */
		/* 当前教学任务课表内容 */
		/* -------------------------------------- */
		
		/* 上课时间 */
		if ( rs_task.task_time EQ '-') { 
			taskTime = ""; 
		}
		else {
			taskTime = rs_task.task_time;
		}

		/* 上课地点 */
		if ( rs_task.task_place EQ '-' ) { 
			taskPlace = "";
		}
		else {
			taskPlace = rs_task.task_place;
		}

		for (i=1; i lte listLen(taskTime, ","); i++) {
			
			currentTaskTime = listGetAt(taskTime, i, ",");
			
			if ( listLen( taskPlace, "," ) gte i ) {
				currentTaskPlace = listGetAt( taskPlace, i, "," );
			}
			else {
				currentTaskPlace = "";
			}
			
			wkday = listGetAt(currentTaskTime, 1, "-@");
			lec = listGetAt(currentTaskTime, 2, "-@");
			cell = listGetAt(currentTaskTime, 3, "-@");

			timeTable[wkday][lec] = -1;

			/* 晚上第5讲安排了3节 则第六讲不可用 */
			if ( cell eq 3 and lec eq 5 ) {
				timeTable[wkday][lec+1] = -2;
			}
			
			/* 4/5节连排 */
			if ( cell eq 4 or cell eq 5 ) {
				timeTable[wkday][lec+1] = -2;
			}
			
			/* 8节连排 */
			if ( listGetAt(currentTaskTime, 3, "-@") eq 8 ) {
				timeTable[wkday][lec+1] = -2;
				timeTable[wkday][lec+2] = -2;
				timeTable[wkday][lec+3] = -2;
			}
			
			/* 
				生成课表内容 
				cellCourse = [ courseName, teacherName, place, cellLength, taskWeek]
			*/
			cellCourse = [ rs_task.course_name,rs_task.tch_name, currentTaskPlace, cell, rs_task.task_week ];
			/* 
				当前任务课表 
				TSK[weekday][lecture] 
			*/
			lectureKey = "TSK" & wkday & lec;
			if ( structKeyExists( timeTableSet, lectureKey ) ) {
				arrayAppend( timeTableSet[lectureKey], cellCourse );
			}
			else {
				structInsert( timeTableSet, lectureKey, [cellCourse] );
			}
			
			
			/* 可用排课节次检查 */
			for( lec=1; lec lte 6; lec++ ) {
				/* 只检查可用讲次 */
				if ( timeTable[wkday][lec] eq 1 ) {

					lectureKey = "CELL" & wkday & lec;
									
					usableCells = timeTableSet[lectureKey];
					checkCells = timeTableSet[lectureKey];

					for ( cell in usableCells ) {

						if ( mathAdvice.isTaskTimeOver( currentTaskTime, wkday & "@" & lec & "-" & cell ) ) {
							arrayDelete( checkCells, cell);
						}
										
					}

					structInsert( timeTableSet, lectureKey, checkCells, true );
									
				}
								
			}

			
		}
		
		/* -------------------------------------- */
		/* 计算教师时间冲突 */
		/* -------------------------------------- */
		sql = "SELECT 
				a.course_name, b.tch_name, c.tasktime, c.task_week, 
				case when c.taskclsroom is null then '-' else c.taskclsroom	end taskclsroom 
				FROM t_teacher_timetable c 
					 INNER JOIN t_course a ON a.cid = c.cid 
					 INNER JOIN t_teacher b ON b.tch_id = c.tch_id 
				WHERE 
					c.term_id= :termId 
					AND 
					c.tch_id = :teacherId 
					AND 
					(c.task_id > :taskId OR c.task_id < :taskId) 
				ORDER BY c.task_id";
				
		queryObj = new Query(datasource = application.dnsSlave);
		queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
		queryObj.addParam(name ="termId", value=termId, cfsqltype="cf_sql_char");
		queryObj.addParam(name ="teacherId", value=rs_task.tch_id, cfsqltype="cf_sql_varchar");
		rs_teacherTask = queryObj.execute(sql = sql).getResult();
		
		for (a = 1; a lte rs_teacherTask.recordCount; a++ ) {
			
			/* 上课时间 */
			currentTaskTime = rs_teacherTask["tasktime"][a];
				
			/* 上课地点 */
			if ( rs_teacherTask["taskclsroom"][a] eq "-" ) { 
				currentTaskPlace = "";
			}
			else {
				currentTaskPlace = rs_teacherTask["taskclsroom"][a];
			}
			
			wkday = listGetAt(currentTaskTime, 1, "-@");
			lec = listGetAt(currentTaskTime, 2, "-@");
			cell = listGetAt(currentTaskTime, 3, "-@");

			/* 
				生成课表内容 
				cellCourse = [ courseName, teacherName, place, cellLength, taskWeek]
			*/
			cellCourse = [ rs_teacherTask["course_name"][a], rs_teacherTask["tch_name"][a], currentTaskPlace, cell, rs_teacherTask["task_week"][a] ];
			/* 
				教师课表 
				TEACHER[weekday][lecture] 
			*/
			lectureKey = "TEACHER" & wkday & lec;
			if ( structKeyExists( timeTableSet, lectureKey ) ) {
				arrayAppend( timeTableSet[lectureKey], cellCourse );
			}
			else {
				structInsert( timeTableSet, lectureKey, [cellCourse] );
			}

			/* 使用周次序列 检验周次重叠情况 */ 
			if ( mathAdvice.isWeekOver( rs_task.task_week, rs_teacherTask["task_week"][a] ) ) {

				timeTable[wkday][lec] = -6;

				/* 晚上第5讲安排了3节 则第六讲不可用 */
				if ( cell eq 3 and lec eq 5 ) {
					timeTable[wkday][lec+1] = -7;
				}
			
				/* 4/5节连排 */
				if ( cell eq 4 or cell eq 5 ) {
					timeTable[wkday][lec+1] = -7;
				}
			
				/* 8节连排 */
				if ( cell eq 8 ) {
					timeTable[wkday][lec+1] = -7;
					timeTable[wkday][lec+2] = -7;
					timeTable[wkday][lec+3] = -7;
				}
				
				/* 课程冲突当天 可用排课节次检查 */
				for( lec=1; lec lte 6; lec++ ) {
					/* 只检查可用讲次 */
					if ( timeTable[wkday][lec] eq 1 ) {

						lectureKey = "CELL" & wkday & lec;
									
						usableCells = timeTableSet[lectureKey];
						checkCells = timeTableSet[lectureKey];

						for ( cell in usableCells ) {

							if ( mathAdvice.isTaskTimeOver( currentTaskTime, wkday & "@" & lec & "-" & cell ) ) {
								arrayDelete( checkCells, cell);
							}
										
						}

						structInsert( timeTableSet, lectureKey, checkCells, true );
									
					}
								
				}

				
			}
			
			
		}
		
		
		/* -------------------------------------- */
		/* 计算课表时间冲突 */
		/* -------------------------------------- */
		
		switch ( rs_task.task_mode ) {
		
			case "1":
			
				/* 按班排课 */
				sql = "SELECT 
						d.course_name, e.tch_name, c.task_time, c.task_week, 
						case when c.task_place is null	then '-' else c.task_place end task_place 
						FROM t_task c
							 INNER JOIN t_course d ON d.cid = c.cid 
							 INNER JOIN t_teacher e ON e.tch_id  = c.tch_id 
						WHERE 
							c.term_id = :termId 
							AND 
							c.task_time is not null 
							AND 
							(c.tsk_id > :taskId or c.tsk_id < :taskId) 
							AND  
							EXISTS (
								SELECT 1
								FROM t_plan b 
								WHERE 
									c.term_id= b.term_id 
									AND 
									c.cid = b.cid
									AND 
									c.cidx = b.cidx 
									AND 
									b.term_id = :termId 
									AND (b.cidx>'XXX' or b.cidx<'XXX') 
									AND (b.cidx>'000' or b.cidx<'000') 
									AND 
									EXISTS (
										SELECT 1 
										FROM t_plan a 
										WHERE 
											a.cls_id = b.cls_id
											AND
											a.term_id = :termId 
											AND 
											a.cid= :courseId 
											AND 
											a.cidx = :courseIdx
									)
								) 
								ORDER BY c.tsk_id";
								
				queryObj = new Query(datasource = application.dnsSlave);
				queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
				queryObj.addParam(name ="termId", value=termId, cfsqltype="cf_sql_char");
				queryObj.addParam(name ="courseId", value=rs_task.cid, cfsqltype="cf_sql_varchar");
				queryObj.addParam(name ="courseIdx", value=rs_task.cidx, cfsqltype="cf_sql_char");
				rs_classTaskConflict = queryObj.execute(sql = sql).getResult();
				
				for (a = 1; a lte rs_classTaskConflict.recordCount; a++) {
					
					/* 如果上课周次有重叠 */
					if ( mathAdvice.isWeekOver( rs_task.task_week, rs_classTaskConflict["task_week"][a] ) ) {
						
						/* 上课时间 */
						taskTime = rs_classTaskConflict["task_time"][a]; /* 1@3-2,7@2-3 */

						/* 上课地点 */
						if ( rs_classTaskConflict["task_place"][a] eq "-" ) { 
							taskPlace = "";
						}
						else {
							taskPlace = rs_classTaskConflict["task_place"][a];
						}

						for (i=1; i lte listLen(taskTime, ","); i++) {
			
							currentTaskTime = listGetAt( taskTime, i, "," );
							
							if ( listLen( taskPlace, "," ) gte i ) {
								currentTaskPlace = listGetAt( taskPlace, i, "," );
							}
							else {
								currentTaskPlace = "";
							}
							
							wkday = listGetAt(currentTaskTime, 1, "-@");
							lec = listGetAt(currentTaskTime, 2, "-@");
							cell = listGetAt(currentTaskTime, 3, "-@");
							
							if ( timeTable[wkday][lec] eq 1 ) {
								timeTable[wkday][lec] = -3;
							}
							
							/* 晚上第5讲安排了3节 则第六讲不可用 */
							if ( cell eq 3 and lec eq 5 ) {
								if ( timeTable[wkday][lec+1] eq 1 ) {
									timeTable[wkday][lec+1] = -4;
								}
							}
			
							/* 4/5节连排 */
							if ( cell eq 4 or cell eq 5 ) {
								if ( timeTable[wkday][lec+1] eq 1 ) {
									timeTable[wkday][lec+1] = -4;
								}
							}
			
							/* 8节连排 */
							if ( cell eq 8 ) {
								if ( timeTable[wkday][lec+1] eq 1 ) {
									timeTable[wkday][lec+1] = -4;
								}
								if ( timeTable[wkday][lec+2] eq 1 ) {
									timeTable[wkday][lec+2] = -4;
								}
								if ( timeTable[wkday][lec+3] eq 1 ) {
									timeTable[wkday][lec+3] = -4;
								}
							}

							/* 
								生成课表内容 
								cellCourse = [ courseName, teacherName, place, cellLength, taskWeek]
							*/
							cellCourse = [ rs_classTaskConflict["course_name"][a], rs_classTaskConflict["tch_name"][a], currentTaskPlace, cell, rs_classTaskConflict["task_week"][a] ];
							/* 
								当前任务课表 
								TSK[weekday][lecture] 
							*/
							lectureKey = "CLASS" & wkday & lec;
							if ( structKeyExists( timeTableSet, lectureKey ) ) {
								arrayAppend( timeTableSet[lectureKey], cellCourse );
							}
							else {
								structInsert( timeTableSet, lectureKey, [cellCourse] );
							}
							
							/* 课程冲突当天 可用排课节次检查 */
							for( lec=1; lec lte 6; lec++ ) {
								/* 只检查可用讲次 */
								if ( timeTable[wkday][lec] eq 1 ) {
									
									lectureKey = "CELL" & wkday & lec;
									
									usableCells = timeTableSet[lectureKey];
									checkCells = timeTableSet[lectureKey];
									
									for ( cell in usableCells ) {
										
										if ( mathAdvice.isTaskTimeOver( currentTaskTime, wkday & "@" & lec & "-" & cell ) ) {
											arrayDelete( checkCells, cell);
										}
										
									}
									
									structInsert( timeTableSet, lectureKey, checkCells, true );
									
								}
								
							}
							

						}
						
						
					}
					
				}


				break;
				
			case "2":
			
				/* 按人排课 */
				sql = "SELECT 
							a.widx, a.tasktime, count(distinct d.stu_id) stu_id
							FROM t_task_timetable a
  								 INNER JOIN t_task_arrange f ON f.task_id = a.task_id , 
                  				 t_student_course d 
							WHERE 
								a.task_id = d.task_id 
								AND 
								f.term_id= :termId  													
								AND
								EXISTS (
									SELECT 1 
									FROM t_student_course e 
									WHERE 
									e.stu_id= d.stu_id
									AND 
									e.task_id = :taskId
								)
								AND 
								(a.task_id > :taskId OR a.task_id < :taskId) 
							GROUP BY 
								a.widx, a.tasktime";
								
				queryObj = new Query(datasource = application.dnsSlave);
				queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
				queryObj.addParam(name ="termId", value=termId, cfsqltype="cf_sql_char");
				rs_studentTaskConflict = queryObj.execute(sql = sql).getResult();
				
				for ( a = 1; a lte rs_studentTaskConflict.recordCount; a++ ) {

					/* 使用周次序列 检验周次重叠情况 */ 
					if ( mathAdvice.isWeekIdxOver( mathAdvice.convertWeekIdx( rs_task.task_week ), rs_studentTaskConflict["widx"][a] ) ) {
						
						currentTaskTime = rs_studentTaskConflict["tasktime"][a];
						studentCount = rs_studentTaskConflict["stu_id"][a];
						
						wkday = listGetAt(currentTaskTime, 1, "-@");
						
						/* 课程冲突当天 可用排课节次检查 */
						for( lec=1; lec lte 6; lec++ ) {
							
							lectureKey = "CELL" & wkday & lec;
							usableCells = timeTableSet[lectureKey];
							
							for ( cell in usableCells ) {
								
								if ( mathAdvice.isTaskTimeOver( currentTaskTime, wkday & "@" & lec & "-" & cell ) ) {
									
									/* 计算学生上课冲突人数 */
									lectureStudentKey = "STUDENT" & wkday & lec & "-" & cell;
									
									if ( structKeyExists( timeTableSet, lectureStudentKey ) ) {
										timeTableSet[lectureStudentKey] += studentCount;
									}
									else {
										structInsert( timeTableSet, lectureStudentKey, studentCount );
									}
									
									/* 标注按人排课上课冲突 */
									if ( timeTable[wkday][lec] eq 1 ) {
										timeTable[wkday][lec] = -5;
									}

								}
								
							}
							
						}

					}
					
				}
				
				/* 计算学生冲突比例 */
				for( lec=1; lec lte 6; lec++ ){
					for( wkday=1; wkday lte 7; wkday++ ){
						
						lectureKey = "CELL" & wkday & lec;
						usableCells = timeTableSet[lectureKey];
						
						for ( cell in usableCells ) {
							
							lectureStudentKey = "STUDENT" & wkday & lec & "-" & cell;
							
							if ( structKeyExists( timeTableSet, lectureStudentKey ) ) {
								
								lectureSummaryKey = "SUMMARY" & wkday & lec;
								
								if ( structKeyExists( timeTableSet, lectureSummaryKey ) ) {
									timeTableSet[lectureSummaryKey] = max( timeTableSet[lectureSummaryKey], timeTableSet[lectureStudentKey] );
								}
								else {
									structInsert( timeTableSet, lectureSummaryKey, timeTableSet[lectureStudentKey] );
								}
								
							}
							
						}
						
					}
				}

				break;
				
		}
		
		
		
		/* -------------------------------------- */
		/* 计算可用教室 */
		/* -------------------------------------- */
		
		if ( rs_taskArrange.period_done lt rs_taskArrange.period_need ) {
			
			/* ----------------- */
			/* 检查是否有可用的教室 */
			/* ----------------- */
			
			/* 计算周次序列和已排周学时 */
			taskWeek = listToArray(rs_task.task_week, "-");
				
			conHSQL = " ";
			for (i=int(taskWeek[1]); i lte int(taskWeek[2]); i++) {
				conHSQL = conHSQL & "SUBSTR(b.widx, " & i & ", 1) = '1' ";
				if (i lt int(taskWeek[2])) { conHSQL = conHSQL & " or "; }
			}
			
			for( lec=1; lec lte 6; lec++ ){

				for( wkday=1; wkday lte 7; wkday++ ){
					
					/* 只检查可用讲次 */
					if ( timeTable[wkday][lec] eq 1 ) {
						
						lectureKey = "CELL" & wkday & lec;
						
						usableCells = timeTableSet[lectureKey];
						checkCells = timeTableSet[lectureKey];

						for ( cell in usableCells ) {
							
							cellStart = mathAdvice.convertLectureIdxToCellStart(lec);
							cellEnd = cellStart + cell -1;
							
							sql = "SELECT 1 
									FROM t_classroom a
									WHERE 
										SUBSTR(a.cr_usage, 2, 1) = '1' 
										AND 
											(
											a.sits_course > :courseSize 
											OR 
											a.sits_course = :courseSize 
											) 
										AND 
										a.cr_type = :clsroomType 
										AND
										a.campus_id = :campusId 
										AND 
										NOT EXISTS (
											SELECT 1 
											FROM t_clsroom_timetable b
											WHERE 
												a.cr_id = b.cr_id
												AND 
												b.term_id = :termId 
												AND 
												b.wdidx = :weekDay 
												AND
												NOT ( b.cellidx + b.cellrelay -1 < :cellStart 
												OR 
												b.cellidx > :cellEnd 
												) 
												AND 
												("&conHSQL&")
										)
									ORDER BY a.sits_course ASC, a.classroom ASC";
									
							queryObj = new Query(datasource = application.dnsSlave,maxRows = 1);
							queryObj.addParam(name ="courseSize", value=rs_task.task_chooesd, cfsqltype="cf_sql_interger");
							queryObj.addParam(name ="termId", value=termId, cfsqltype="cf_sql_char");
							queryObj.addParam(name ="weekDay", value=wkday, cfsqltype="cf_sql_char");
							queryObj.addParam(name ="cellStart", value=cellStart, cfsqltype="cf_sql_interger");
							queryObj.addParam(name ="cellEnd", value=cellEnd, cfsqltype="cf_sql_interger");
							queryObj.addParam(name ="clsroomType", value=rs_task.cr_type, cfsqltype="cf_sql_char");
							queryObj.addParam(name ="campusId", value=rs_task.campus_id, cfsqltype="cf_sql_char");
							rs_clsroomCheck = queryObj.execute(sql = sql).getResult();
							
							/* 如果没有可用教室 */
							if (rs_clsroomCheck.recordCount eq 0) {
								arrayDelete( checkCells, cell);
							}

						}
						structInsert( timeTableSet, lectureKey, checkCells, true );
					}
					
				
				}
			}
			
			

		}

		/* 生成返回地址 */
		period = event.getArg("PR");
		filter = event.getArg("FT");
		depId = event.getArg("DEP");
		courseProp = event.getArg("CP");

		backArgs = structNew();
		
		structInsert( backArgs, "TID", termId, true );
		structInsert( backArgs, "CID", rs_task.cid, true );
		
		if (period neq "") { structInsert( backArgs, "PR", period, true ); }
		if (filter neq "") { structInsert( backArgs, "FT", filter, true ); }
		if (depId neq "") { structInsert( backArgs, "DEP", depId, true ); }
		if (courseProp neq "") { structInsert( backArgs, "CP", courseProp, true ); }

		/* 对于 javascript 地址 &amp; 需要重新转义为 & 避免 google chrome 浏览器无法正确识别地址 */
		switch (taskType) {

			case "P":
				backURL = replace( buildURL( "planTask", backArgs), "&amp;", "&", "all");
				break;

			case "C":
				backURL = replace( buildURL( "commonTask", backArgs), "&amp;", "&", "all");
				break;
			
			case "R":
				backURL = replace( buildURL( "openTask", backArgs), "&amp;", "&", "all");
				break;
			
			case "T":
				backURL = replace( buildURL( "sportTask", backArgs), "&amp;", "&", "all");
				break;
				
		}
		
	</cfscript>
</cfsilent>

<div class="noticeBlock">

	<h3>《<cfoutput>#rs_task.course_name#</cfoutput>》 安排上课时间地点</h3>
	<p>
		
		任课单位: <cfoutput>#rs_task.institute_name#</cfoutput> 
		总学时 <em><cfoutput>#rs_task.period#</cfoutput></em>, 其中理论学时 <em><cfoutput>#rs_task.period_theory#</cfoutput></em>. 
		课程总学分:<em><cfoutput>#numberFormat(rs_task.course_credit, '_.00')#</cfoutput></em>
		
		排课学时: <em><cfoutput>#rs_taskArrange.period_done#</cfoutput> / <cfoutput>#rs_taskArrange.period_need#</cfoutput></em>
	</p>
	<p>	
		<span class="img icon16x16 message"></span>
		主讲教师: <cfoutput>#rs_task.tch_name#</cfoutput><em>(<cfoutput>#rs_task.tch_id#</cfoutput>)</em>
		排课周次: <em><cfoutput>#rs_task.task_week#</cfoutput></em> 
		周学时: <em><cfoutput>#rs_task.task_period#</cfoutput></em>
		
		<cfif rs_task.task_desc neq "">排课要求: <cfoutput>#rs_task.task_desc#</cfoutput></cfif>
	</p>
	<p>	
		<span class="img icon16x16 building"></span>
		校区: <cfoutput>#rs_task.campus_name#</cfoutput>
		教学场地: <cfoutput>#rs_task.cr_type_name#</cfoutput>
		
		实际人数: <em><cfoutput>#rs_task.task_chooesd#</cfoutput></em>
		计划人数: <em><cfoutput>#rs_task.task_limite#</cfoutput></em>

		<cfif taskType eq "P">
		<span class="img icon16x16 card"></span>
		排课班级: <cfoutput>#rs_task.task_classes#</cfoutput>
		</cfif>
	</p>
	
	<hr/>
	
	<div class="UIArrangeOper mtl">
		<b class="lable">设置</b>
		<div class="content"> <span class="opTt">校区</span>
            <cfloop query="rs_campus"  >
				<cfif rs_task.campus_id eq rs_campus.campus_id>
					<span class="opBtn OptOn"><cfoutput>#rs_campus.campus_name#</cfoutput></span>
					<cfelse>
					<a class="opBtn Opt" href="javascript:setCampus('<cfoutput>#rs_campus.campus_id#</cfoutput>');"><cfoutput>#rs_campus.campus_name#</cfoutput></a>
				</cfif>
			</cfloop>
			&nbsp;&nbsp;&nbsp;&nbsp;
			<cfif taskType eq "P">
				<span class="opTt">排课模式</span>
				<cfif rs_task.task_mode eq "1">
					<span class="opBtn OptOn">按班</span> <a class="opBtn Opt" href="javascript:setArrangeMode(2);">按人</a>
				</cfif>
				<cfif rs_task.task_mode eq "2">
					<a class="opBtn Opt" href="javascript:setArrangeMode(1);">按班</a> <span class="opBtn OptOn">按人</span>
				</cfif>
				&nbsp;&nbsp;&nbsp;&nbsp;
			</cfif>
			<!--- 清除排课 ---> 
			<a class="opBtn Spe" href="javascript:clearTask('<cfoutput>#taskId#</cfoutput>', '<cfoutput>#taskType#</cfoutput>');">清除教学班排课结果</a>
		</div>
	</div>
	
	<div class="UIArrangeOper">
		<b class="lable">教室</b>
		<div class="content">
            <cfloop  query="rs_clsroomType" >
				<cfif rs_task.cr_type eq rs_clsroomType.cr_type>
							<span class="opBtn OptOn"><cfoutput>#rs_clsroomType.cr_type_name#</cfoutput></span>
						<cfelse>
							<a class="opBtn Opt" href="javascript:setClassroomType('<cfoutput>#rs_clsroomType.cr_type#</cfoutput>');"><cfoutput>#rs_clsroomType.cr_type_name#</cfoutput></a>
					</cfif>
			</cfloop>
		</div>
		<div class="content">
            <cfloop query="rs_clsroomTypeSpecial"  >
				<cfif rs_task.cr_type eq rs_clsroomTypeSpecial.cr_type>
							<span class="opBtn OptOn"><cfoutput>#rs_clsroomTypeSpecial.cr_type_name#</cfoutput></span>
						<cfelse>
							<a class="opBtn Opt" href="javascript:setClassroomType('<cfoutput>#rs_clsroomTypeSpecial.cr_type#</cfoutput>');"><cfoutput>#rs_clsroomTypeSpecial.cr_type_name#</cfoutput></a>
					</cfif>
			</cfloop>
		</div>
	</div>
	
	<div class="UIArrangeOper mbl">
		<b class="lable">调整</b>
		<div class="content">
		
			<cfif rs_taskArrange.period_done eq 0>
				<span>调整周学时</span>
				<select name="TaskPeriod" id="TaskPeriod" class="filedPeriod">
					<cfloop list="2,1,3,2+2,4,3+2,5,3+3,4+2,2+2+2,3+3+2,4+4,2+2+2+2,8,3+3+2+2,2+2+2+2+2,2+2+2+2+2+2,4+4+4+4" delimiters="," index="pid">
						<option <cfif rs_task.task_period eq pid>selected="selected"</cfif> value="<cfoutput>#pid#</cfoutput>"><cfoutput>#pid#</cfoutput></option>
					</cfloop>
				</select>
				起始周 <input name="WeekStart" id="WeekStart" type="text" class="filedWeek" maxlength="2" value="<cfoutput>#listFirst( rs_task.task_week, '-')#</cfoutput>" />
				结束周 <input name="WeekEnd" id="WeekEnd" type="text" class="filedWeek" maxlength="2" value="<cfoutput>#listLast( rs_task.task_week, '-')#</cfoutput>" /><input id="taskWeekPriodTrigger" class="btnSubmit" type="button" value="更新" />
				&nbsp;&nbsp;&nbsp;&nbsp;
			</cfif>

			<!-- formChangeSize -->
			<span>调整计划人数</span><input class="filedPlanSize" id="taskSize" type="text" maxlength="4" value="<cfoutput>#rs_task.task_limite#</cfoutput>" /><input id="taskSizeTrigger" class="btnSubmit" type="button" value="保存" />
			<!-- /formChangeSize -->
		</div>
	</div>
	
	<hr/>
	
</div>

<table class="UICourseTable">
	<thead>
		<tr>
			<td colspan="2" width="2%"></td>
			<td width="14%">星期一</td>
			<td width="14%">星期二</td>
			<td width="14%">星期三</td>
			<td width="14%">星期四</td>
			<td width="14%">星期五</td>
			<td width="14%">星期六</td>
			<td width="14%">星期天</td>
		</tr>
	</thead>
	<tbody>
		<cfloop from="1" to="6" index="lec">
		
		<tr>
			<cfswitch expression="#lec#">
				<cfcase value="1">
					<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
				</cfcase>
				<cfcase value="3">
					<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
				</cfcase>
				<cfcase value="5">
					<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
				</cfcase>
				</cfswitch>
				<td><cfoutput>#mathAdvice.convertLectureIdxToChinese(lec)#</cfoutput></td>
			
				<cfloop from="1" to="7" index="wkday">
						
					<!--- 计算冲突比例 并高亮显示 --->
					<cfset appendClass = "" />
					<cfset lectureSummaryKey = "SUMMARY" & wkday & lec />
					<cfif structKeyExists( timeTableSet, lectureSummaryKey ) and rs_task.task_chooesd gt 0>
						<cfset prog = 100 * timeTableSet[lectureSummaryKey] / rs_task.task_chooesd />
						
						<cfif prog gt 0 and prog lte 10><cfset appendClass = "conflictL1" /></cfif>
						<cfif prog gt 10 and prog lte 20><cfset appendClass = "conflictL2" /></cfif>
						<cfif prog gt 20 and prog lte 30><cfset appendClass = "conflictL3" /></cfif>
						<cfif prog gt 30 and prog lte 60><cfset appendClass = "conflictL4" /></cfif>
						<cfif prog gt 60 and prog lte 100><cfset appendClass = "conflictL5" /></cfif>
						
					</cfif>
					
					<td id="tbcTime<cfoutput>#wkday#</cfoutput><cfoutput>#lec#</cfoutput>" class="<cfoutput>#appendClass#</cfoutput>">
					

						<cfswitch expression="#timeTable[wkday][lec]#">
							
							<cfcase value="1">
								<!--- 如果可排课 且学时未排够 --->
								<cfif rs_taskArrange.period_done lt rs_taskArrange.period_need>
									<cfset lectureKey = "CELL" & wkday & lec />
                                             <cfset writeLOg( text=lectureKey )/>
									<cfif structKeyExists( timeTableSet, lectureKey )>
										<cfset c = 0 />
										<cfloop array="#timeTableSet[lectureKey]#" index="allowCell">
											<cfset c++ />
											<a class="cellCount" href="javascript:addTimePlace(<cfoutput>#wkday#</cfoutput>, <cfoutput>#lec#</cfoutput>, <cfoutput>#allowCell#</cfoutput>);"><cfoutput>#allowCell#</cfoutput></a>
											<cfif c lt arrayLen(timeTableSet[lectureKey])>&nbsp;</cfif>
										</cfloop>
									</cfif>
								</cfif>
								
								<!--- 显示教师课表 --->
								<cfset lectureKey = "TEACHER" & wkday & lec />
								<cfif structKeyExists( timeTableSet, lectureKey )>
									</br></br>
									<cfset c = 0 />
									<cfloop array="#timeTableSet[lectureKey]#" index="cellCourse">
										<cfset c++ />
										<!-- 最多显示2项 -->
										<cfif c lt 3>
											<span>
												<cfoutput>#cellCourse[1]#</cfoutput>
												-
												<cfoutput>#cellCourse[2]#</cfoutput>
											</span>
											<br/>
											<span class="numeric"><cfoutput>#cellCourse[5]#</cfoutput>(<cfoutput>#cellCourse[4]#</cfoutput>)</span>
											<cfif cellCourse[3] neq "">
												<p class="desc">
													<cfoutput>#cellCourse[3]#</cfoutput>
												</p>
											</cfif>
											<cfif c lt 2 and c lt arrayLen(timeTableSet[lectureKey])><span class="numeric">------------</span></br></cfif>
										</cfif>
									</cfloop>
								</cfif>
								
							</cfcase>
							
							<cfcase value="-1">
								<!--- 如果已排课 --->
								<cfset lectureKey = "TSK" & wkday & lec />
								<cfif structKeyExists( timeTableSet, lectureKey )>
									<cfloop array="#timeTableSet[lectureKey]#" index="cellCourse">
										<a href="javascript:removeTimeCell(<cfoutput>#wkday#</cfoutput>, <cfoutput>#lec#</cfoutput>, <cfoutput>#cellCourse[4]#</cfoutput>);">
											<cfoutput>#cellCourse[1]#</cfoutput>
											-
											<cfoutput>#cellCourse[2]#</cfoutput>
										</a>
										<br/>
										<span class="numeric"><cfoutput>#cellCourse[5]#</cfoutput>(<cfoutput>#cellCourse[4]#</cfoutput>)</span>
										<cfif cellCourse[3] neq "">
											<p class="desc">
												<cfoutput>#cellCourse[3]#</cfoutput>
											</p>
										</cfif>
									</cfloop>
								</cfif>
							</cfcase>
							
							<cfcase value="-3">
								<!--- 如果班级课表冲突 --->
								<cfset lectureKey = "CLASS" & wkday & lec />
								<cfif structKeyExists( timeTableSet, lectureKey )>
									<cfset c = 0 />
									<cfloop array="#timeTableSet[lectureKey]#" index="cellCourse">
										<cfset c++ />
										<!-- 最多显示2项 -->
										<cfif c lt 3>
											<span>
												<cfoutput>#cellCourse[1]#</cfoutput>
												-
												<cfoutput>#cellCourse[2]#</cfoutput>
											</span>
											<br/>
											<span class="numeric"><cfoutput>#cellCourse[5]#</cfoutput>(<cfoutput>#cellCourse[4]#</cfoutput>)</span>
											<cfif cellCourse[3] neq "">
												<p class="desc">
													<cfoutput>#cellCourse[3]#</cfoutput>
												</p>
											</cfif>
											<cfif c lt 2 and c lt arrayLen(timeTableSet[lectureKey])><span class="numeric">------------</span></br></cfif>
										</cfif>
									</cfloop>
								</cfif>
							</cfcase>
							
							<cfcase value="-5">
								<!--- 学生课表冲突 --->
								<cfset lectureKey = "CELL" & wkday & lec />
								<cfif structKeyExists( timeTableSet, lectureKey )>
									
									<cfset c = 0 />
									<cfloop array="#timeTableSet[lectureKey]#" index="allowCell">
										<cfset c++ />
										<a class="cellCount" href="javascript:addTimePlace(<cfoutput>#wkday#</cfoutput>, <cfoutput>#lec#</cfoutput>, <cfoutput>#allowCell#</cfoutput>);"><cfoutput>#allowCell#</cfoutput></a>
										<cfset lectureStudentKey = "STUDENT" & wkday & lec & "-" & allowCell />
										<cfif structKeyExists( timeTableSet, lectureStudentKey )>
											<span class="numeric">(<cfoutput>#timeTableSet[lectureStudentKey]#</cfoutput>)</span>
										</cfif>
										<cfif c lt arrayLen(timeTableSet[lectureKey])>
											<br/><span class="numeric">------------</span></br>
										</cfif>
									</cfloop>
								</cfif>
							</cfcase>
							
							<cfcase value="-6">
								<!--- 如果教师课表冲突 --->
								<cfset lectureKey = "TEACHER" & wkday & lec />
								<cfif structKeyExists( timeTableSet, lectureKey )>
									<cfset c = 0 />
									<cfloop array="#timeTableSet[lectureKey]#" index="cellCourse">
										<cfset c++ />
										<!-- 最多显示2项 -->
										<cfif c lt 3>
											<span class="notice">
												<cfoutput>#cellCourse[1]#</cfoutput>
												-
												<cfoutput>#cellCourse[2]#</cfoutput>
											</span>
											<br/>
											<span class="numeric"><cfoutput>#cellCourse[5]#</cfoutput>(<cfoutput>#cellCourse[4]#</cfoutput>)</span>
											<cfif cellCourse[3] neq "">
												<p class="desc">
													<cfoutput>#cellCourse[3]#</cfoutput>
												</p>
											</cfif>
											<cfif c lt 2 and c lt arrayLen(timeTableSet[lectureKey])><span class="numeric">------------</span></br></cfif>
										</cfif>
									</cfloop>
								</cfif>
							</cfcase>
							
							
						</cfswitch>
						

					</td>
					
				</cfloop>
			</tr>
		</cfloop>
	</tbody>
</table>

<!---<div id="out"></div>--->

<script language="javascript" type="text/javascript">
	//<![CDATA[
	<!--
	function setArrangeMode(arrangeMode) {
		
		/* 显示阻挡信息 */
		$.blockUI({ 
					message: '更改排课模式...',
					css: { 
							'width': '16%',
							'left': '42%',
							'border': 'none', 
							'padding': '15px 5px', 
							'backgroundColor': '#090', 
							'border-radius': '4px', 
							'opacity': .8, 
							'color': '#fff',
							'cursor': null
							},
					overlayCSS: {
							'backgroundColor': '#000', 
							'opacity': .1 ,
							'cursor': null
							}
					}); 

		$.post(
				"<cfoutput>#buildURL('apiTaskSetArrangeMode')#</cfoutput>", 
				{
					'mode' : arrangeMode,
					'tt' : '<cfoutput>#taskType#</cfoutput>',
					'tsk' : '<cfoutput>#taskId#</cfoutput>',
					'seed': (new Date()).getTime()
				}, 
				function(data) {
					
					//$("#out").html(data);
					
					result = $.parseJSON(data);
										
					/* 刷新排课数据 */
					window.location.reload();
				});

	}
	
	function setCampus(campusId) {
		
		/* 显示阻挡信息 */
		$.blockUI({ 
					message: '更改校区...',
					css: { 
							'width': '16%',
							'left': '42%',
							'border': 'none', 
							'padding': '15px 5px', 
							'backgroundColor': '#090', 
							'border-radius': '4px', 
							'opacity': .8, 
							'color': '#fff',
							'cursor': null
							},
					overlayCSS: {
							'backgroundColor': '#000', 
							'opacity': .1 ,
							'cursor': null
							}
					}); 
		
		$.post(
				"<cfoutput>#buildURL('apiTaskSetCampus')#</cfoutput>", 
				{
					'capm' : campusId,
					'tt' : '<cfoutput>#taskType#</cfoutput>',
					'tsk' : '<cfoutput>#taskId#</cfoutput>',
					'seed': (new Date()).getTime()
				}, 
				function(data) {
					
					//$("#out").html(data);

					result = $.parseJSON(data);
										
					/* 刷新排课数据 */
					window.location.reload();
				});
		
	}
	
	function setClassroomType(clsroomType) {
		
		/* 显示阻挡信息 */
		$.blockUI({ 
					message: '更改教室类别...',
					css: { 
							'width': '16%',
							'left': '42%',
							'border': 'none', 
							'padding': '15px 5px', 
							'backgroundColor': '#090', 
							'border-radius': '4px', 
							'opacity': .8, 
							'color': '#fff',
							'cursor': null
							},
					overlayCSS: {
							'backgroundColor': '#000', 
							'opacity': .1 ,
							'cursor': null
							}
					}); 
		
		$.post(
				"<cfoutput>#buildURL('apiTaskSetClassroomType')#</cfoutput>", 
				{
					'crtp' : clsroomType,
					'tt' : '<cfoutput>#taskType#</cfoutput>',
					'tsk' : '<cfoutput>#taskId#</cfoutput>',
					'seed': (new Date()).getTime()
				}, 
				function(data) {
					
					//$("#out").html(data);

					result = $.parseJSON(data);
										
					/* 刷新排课数据 */
					window.location.reload();
				});
		
	}
	
	function removeTimeCell(weekDay, lecture, cells){
		
		/* 显示阻挡信息 */
		$.blockUI({ 
					message: '删除上课时间...',
					css: { 
							'width': '16%',
							'left': '42%',
							'border': 'none', 
							'padding': '15px 5px', 
							'backgroundColor': '#900', 
							'border-radius': '4px', 
							'opacity': .8, 
							'color': '#fff',
							'cursor': null
							},
					overlayCSS: {
							'backgroundColor': '#000', 
							'opacity': .1 ,
							'cursor': null
							}
					}); 
		
		$.post(
				"<cfoutput>#buildURL('apiTaskClearTime')#</cfoutput>", 
				{
					'wkday' : weekDay,
					'lec' : lecture,
					'n' : cells,
					'tt' : '<cfoutput>#taskType#</cfoutput>',
					'tsk' : '<cfoutput>#taskId#</cfoutput>',
					'seed': (new Date()).getTime()
				}, 
				function(data) {
					
					//$("#out").html(data);
					
					result = $.parseJSON(data);
										
					/* 刷新排课数据 */
					window.location.reload();
				});
		
		
	}
	
	function addTimeCell(weekDay, lecture, cells){
		
		/* 显示阻挡信息 */
		$.blockUI({ 
					message: '安排上课时间...',
					css: { 
							'width': '16%',
							'left': '42%',
							'border': 'none', 
							'padding': '15px 5px', 
							'backgroundColor': '#F90', 
							'border-radius': '4px', 
							'opacity': .8, 
							'color': '#fff',
							'cursor': null
							},
					overlayCSS: {
							'backgroundColor': '#000', 
							'opacity': .1 ,
							'cursor': null
							}
					}); 
		
		$.post(
				"<cfoutput>#buildURL('apiTaskSetTime')#</cfoutput>", 
				{
					'wkday' : weekDay,
					'lec' : lecture,
					'n' : cells,
					'tt' : '<cfoutput>#taskType#</cfoutput>',
					'tsk' : '<cfoutput>#taskId#</cfoutput>',
					'seed': (new Date()).getTime()
				}, 
				function(data) {
					
					//$("#out").html(data);

					result = $.parseJSON(data);
					
					if (result.success) {

						if ( parseInt(result.periodDone) >= parseInt(result.periodNeed) ) {
							/* 返回排课任务列表 */
							window.location = '<cfoutput>#backURL#</cfoutput>';
						}
						else {
							/* 刷新排课数据 */
							window.location.reload();
						}
					
					}
					else {
						
						/* 取消阻挡 */
						$.unblockUI();
						
						alert(result.reason);
					}

				});
		
	}
	
	function addTimePlace(weekDay, lecture, cells){
		
		/* 显示阻挡信息 */
		$.blockUI({ 
					message: '安排上课时间及地点...',
					css: { 
							'width': '16%',
							'left': '42%',
							'border': 'none', 
							'padding': '15px 5px', 
							'backgroundColor': '#F60', 
							'border-radius': '4px', 
							'opacity': .8, 
							'color': '#fff',
							'cursor': null
							},
					overlayCSS: {
							'backgroundColor': '#000', 
							'opacity': .1 ,
							'cursor': null
							}
					}); 
		
		$.post(
				"<cfoutput>#buildURL('apiTaskSetTimePlace')#</cfoutput>", 
				{
					'wkday' : weekDay,
					'lec' : lecture,
					'n' : cells,
					'tt' : '<cfoutput>#taskType#</cfoutput>',
					'tsk' : '<cfoutput>#taskId#</cfoutput>',
					'seed': (new Date()).getTime()
				}, 
				function(data) {
					
					//$("#out").html(data);

					result = $.parseJSON(data);
					
					if (result.success) {
						
						if ( parseInt(result.periodDone) >= parseInt(result.periodNeed) ) {
							/* 返回排课任务列表 */
							window.location = '<cfoutput>#backURL#</cfoutput>';
						}
						else {
							/* 刷新排课数据 */
							window.location.reload();
						}
						
					}
					else {
						
						/* 取消阻挡 */
						$.unblockUI();

						$("#tbcTime" + result.weekDay + result.lecture).html("<span>无可用教室</span>");

						alert(result.reason);
						
					}
					
					
				});
	}
	
	function clearTask( taskId, taskType ) {
		
		/* 显示阻挡信息 */
		$.blockUI({ 
					message: '删除课程排课...',
					css: { 
							'width': '16%',
							'left': '42%',
							'border': 'none', 
							'padding': '15px 5px', 
							'backgroundColor': '#900', 
							'border-radius': '4px', 
							'opacity': .8, 
							'color': '#fff',
							'cursor': null
							},
					overlayCSS: {
							'backgroundColor': '#000', 
							'opacity': .1 ,
							'cursor': null
							}
					}); 
				
		$.post(
				"<cfoutput>#buildURL('apiTaskClear')#</cfoutput>", 
				{
					'TSK' : taskId,
					'TT' : taskType,
					'seed': (new Date()).getTime()
				}, 
				function(data) {

					result = $.parseJSON(data);
											
					if (result.success) {
						/* 刷新排课数据 */
						window.location.reload();
					}
					else {
						alert(result.reason);
					}
											
				});
				
	}
	
	function changeTaskWeekPriod() {

		var taskPeriod = $("#TaskPeriod").val();
		var taskWeekStart = $("#WeekStart").val();
		var taskWeekEnd = $("#WeekEnd").val();
				
		if ( taskWeekEnd > taskWeekStart ) {
			
			/* 显示阻挡信息 */
			$.blockUI({ 
						message: '更改周次安排...',
						css: { 
								'width': '16%',
								'left': '42%',
								'border': 'none', 
								'padding': '15px 5px', 
								'backgroundColor': '#090', 
								'border-radius': '4px', 
								'opacity': .8, 
								'color': '#fff',
								'cursor': null
								},
						overlayCSS: {
								'backgroundColor': '#000', 
								'opacity': .1 ,
								'cursor': null
								}
						});
				
			$.post(
					"<cfoutput>#buildURL('apiTaskWeekPeriodChange')#</cfoutput>",
					{
						'TSK' : '<cfoutput>#taskId#</cfoutput>',
						'TT' : '<cfoutput>#taskType#</cfoutput>',
						'TP' : taskPeriod,
						'TWS' : taskWeekStart,
						'TWE' : taskWeekEnd,
						'seed': (new Date()).getTime()
					},
					function(data) {
						//$("#out").html(data);
						result = $.parseJSON(data);
	
						if ( result.success ) {
							/* 刷新排课数据 */
							window.location.reload();
						}
							
					});
		}
				
	}
			
	function changePlanSize() {
				
		var taskSize = $("#taskSize").val();
		var planSize = parseInt(taskSize);
		var step = planSize - <cfoutput>#rs_task.task_limite#</cfoutput>;

		if ( planSize >= <cfoutput>#rs_task.task_chooesd#</cfoutput> ) {
			
			/* 显示阻挡信息 */
			$.blockUI({ 
						message: '更改计划人数...',
						css: { 
								'width': '16%',
								'left': '42%',
								'border': 'none', 
								'padding': '15px 5px', 
								'backgroundColor': '#090', 
								'border-radius': '4px', 
								'opacity': .8, 
								'color': '#fff',
								'cursor': null
								},
						overlayCSS: {
								'backgroundColor': '#000', 
								'opacity': .1 ,
								'cursor': null
								}
						});

			$.post(
					"<cfoutput>#buildURL('apiTaskPlanSizeChange')#</cfoutput>",
					{
						'TSK' : '<cfoutput>#taskId#</cfoutput>',
						'TT' : '<cfoutput>#taskType#</cfoutput>',
						'STP' : step,
						'seed': (new Date()).getTime()
					},
					function(data) {
						
						$("#out").html(data);
						
						result = $.parseJSON(data);

						if ( result.success ) {
							/* 刷新排课数据 */
							window.location.reload();
						}
											
					});

		}
		else {
			alert("小于实际选课人数");
		}

	}
			
	$(document).ready(function() {
				
		/* 绑定事件相应 */
		$("#taskSizeTrigger").click(function(){
			changePlanSize();
		});

		$("#taskWeekPriodTrigger").click(function(){
			changeTaskWeekPriod();
		});
				

		$("#taskSize").keydown(function(event) {
			var keyCode = event.keyCode;
			if (keyCode == 13) {
				changePlanSize();
			}
		});

	});
	
	// -->
	//]]>
</script>